<?php

// Namespace

namespace Snsfg;


// Dependencies

require_once(PATH_LOOP . 'Model/CUser.php');


/**
 * @package Qa
 * @category Model
 */

class CUser extends \Loop\CUser
{
	/**
	 *
	 */

	protected function createProperties()
	{
		parent::createProperties();

		$this->createProperty
		(
			'socialNetwork',
			array
			(
				'isName' => true,
				'type' => TYPE_STRING_LONG
			)
		);

		$this->createProperty
		(
			'nickname',
			array
			(
				'isName' => true,
				'type' => TYPE_STRING_LONG
			)
		);

		$this->createProperty
		(
			'score',
			array
			(
				'type' => TYPE_INTEGER
			)
		);
	}


	/**
	 *
	 */


	public function api_register()
	{
		// Get API arguments

		$userNickname = fv('userNickname', true);
		$userEmail = fv('userEmail', true);
		$userPassword = fv('userPassword', true);
		$userConfirmPassword = fv('userConfirmPassword', true);


		// Call method

		$result = $this->register($userNickname, $userEmail, $userPassword, $userConfirmPassword, null);

		return $result;
	}


	/**
	 *
	 */

	public function register($userNickname, $userEmail, $userPassword, $userConfirmPassword, $socialNetwork = null)
	{

		if (empty($userNickname) === true)
		{
			e
			(
				EXCEPTION_USER_NICKNAME_NEEDED
			);
		}

		if (empty($userEmail) === true)
		{
			e
			(
				EXCEPTION_USER_EMAIL_NEEDED
			);
		}

		if (empty($userPassword) === true)
		{
			e
			(
				EXCEPTION_USER_PASSWORD_NEEDED
			);
		}

		if (empty($userConfirmPassword) === true)
		{
			e
			(
				EXCEPTION_USER_CONFIRM_PASSWORD_NEEDED
			);
		}

		if ($userPassword != $userConfirmPassword)
		{
			e
			(
				EXCEPTION_USER_PASSWORD_UNMATCH
			);
		}


		//

		$rawUsers = $this->find
		(
			array
			(
				'conditions' => array
				(
					'nickname' => array(EQUAL, $userNickname)
				)
			)
		);

		if (count($rawUsers) != 0)
		{
			e
			(
				EXCEPTION_USER_NICKNAMW_ALREADY_EXISTS,
				array('nickname' => $userNickname)
			);
		}
		
		//

		$rawUsers = $this->find
		(
			array
			(
				'conditions' => array
				(
					'email' => array(EQUAL, $userEmail)
				)
			)
		);

		if (count($rawUsers) != 0)
		{
			e
			(
				EXCEPTION_USER_EMAIL_ALREADY_EXISTS,
				array('userEmail' => $userEmail)
			);
		}


		// Set the user properties
		
		$this->setNickname($userNickname);
		$this->setSocialNetwork($socialNetwork);
		$this->setEmail($userEmail);
		$this->setEncryptedPassword(app()->getStringHelper()->encrypt($userPassword));
		
		
		// Save the user
		
		$this->save();
		
	}

}

?>
